package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service // spring的
public class UserManager {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private MovementService movementService;

    @Reference
    private CommentService commentService;

    @Reference
    private VideoService videoService;

    public ResponseEntity findUserByPage(Integer pageNum, Integer pageSize) {
        // 调用rpc查询
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);

        // 返回分页结果
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findUserInfoById(Long userID) {
        // 调用rpc查询
        UserInfo userInfo = userInfoService.findById(userID);

        // 返回结果
        return ResponseEntity.ok(userInfo);
    }

    public ResponseEntity findMovementVoByPage(Long uid, Integer stateInt, Integer pageNum, Integer pageSize) {
        // 1.调用rpc完成分页查询
        PageBeanVo pageBeanVo = movementService.findMovementByCondition(uid, stateInt, pageNum, pageSize);

        // 2.封装vo
        List<MovementVo> voList = new ArrayList<>();
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                // 查询userInfo
                UserInfo userInfo = userInfoService.findById(movement.getUserId());
                // 创建vo
                MovementVo movementVo = new MovementVo();

                // 设置vo，顺序不能错。。。
                movementVo.setUserInfo(userInfo);
                movementVo.setMovement(movement);
                // 单独处理时间问题。。。
                movementVo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
                // 设置volist
                voList.add(movementVo);
            }
        }

        // 3.返回分页对象
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMovementVoById(String movementId) {
        // 1.调用id查询动态
        Movement movement = movementService.findMovementById(movementId);
        // 2.封装vo
        // 2-1 查询userInfo
        UserInfo userInfo = userInfoService.findById(movement.getUserId());
        // 2-2 创建vo
        MovementVo movementVo = new MovementVo();
        // 2-3 设置vo，顺序不能错。。。
        movementVo.setUserInfo(userInfo);
        movementVo.setMovement(movement);
        // 2-4 单独处理时间问题。。。
        movementVo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));
        // 3.返回结果
        return ResponseEntity.ok(movementVo);
    }

    public ResponseEntity findMovementComemntByPage(String movementId, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询评论分页对象
        PageBeanVo pageBeanVo = commentService.findMovementCommentByPage(pageNum, pageSize, movementId);
        // 2.封装CommentVo
        List<CommentVo> voList = new ArrayList<>();
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 获取评论人id
                Long userId = comment.getUserId();
                // 获取评论人信息
                UserInfo userInfo = userInfoService.findById(userId);

                //  创建vo
                CommentVo commentVo = new CommentVo();

                // 组装info信息
                commentVo.setAvatar(userInfo.getAvatar());
                commentVo.setNickname(userInfo.getNickname());

                // 组装comment信息
                commentVo.setId(comment.getId().toString());
                commentVo.setContent(comment.getContent());
                commentVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(comment.getCreated())));
                // 添加到集合中
                voList.add(commentVo);
            }
        }

        // 3.返回pageBeanVo
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findVideoVoByPage(Long uid, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = videoService.findMyVideoByPage(uid, pageNum, pageSize);

        // 2.封装videoVo
        List<VideoVo> voList = new ArrayList<>();
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                // 获取发布视频用户详情
                UserInfo userInfo = userInfoService.findById(video.getUserId());

                // 创建并封装vo
                VideoVo videoVo = new VideoVo();
                BeanUtil.copyProperties(userInfo, videoVo);// 用户信息
                BeanUtil.copyProperties(video, videoVo);// 视频信息
                // 封装其他补充字段
                videoVo.setCover(video.getPicUrl()); // 视频封面
                videoVo.setSignature(video.getText()); // 视频文字

                // 设置到voList
                voList.add(videoVo);

            }
        }

        // 3.返回结果
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
